'''
Not mine, I found it by searching "pygame signal" on Google
https://www.pygame.org/wiki/SignalSlot

Original description:
A simple implementation of the Signal/Slot pattern. I originally uploaded this to ASPN's python cookbook in 2005. To use, simply create a Signal instance and connect() methods, which act as the "slot" in this design pattern. The instance of signal is self-sufficient; it doesn't have to be a member of a class. Connect slots to the signal using the "connect()" method. The slot may be a member of a class or a simple function. Signal uses weak references, so it will not prevent collection of objects simply because the object is connected to a Signal.

This pattern is useful for an event system, entity communication, gui systems, or any other system that needs objects to communicate without tight coupling.

See also [PyDispatcher](https://pydispatcher.sourceforge.net/), a more fully developed version of the same thing. 
'''

class Signal:
    def __init__(self):
        self.slots = []

    def __call__(self, *args, **kwargs):
        for i, slot in enumerate(self.slots):
            if slot != None:
                slot(*args, **kwargs)
            else:
                del self.slots[i]

    def call(self, *args, **kwargs):
        self.__call__(*args, **kwargs)

    def emit(self, *args, **kwargs):
        self.call(*args, **kwargs)

    def connect(self, slot):
        self.disconnect(slot)
        self.slots.append(slot)

    def disconnect(self, slot):
        try:
            for i, wm in enumerate(self.slots):
                if wm == slot:
                    del self.slots[i]
                    return
        except:
            pass

    def disconnectAll(self):
        self.slots = []
